<?php

/**
 * This is the model class for table "{{countries}}".
 *
 * The followings are the available columns in table '{{countries}}':
 * @property integer $idCountry
 * @property string $countryCode
 * @property string $countryName
 * @property string $currencyCode
 * @property string $population
 * @property string $fipsCode
 * @property string $isoNumeric
 * @property string $north
 * @property string $south
 * @property string $east
 * @property string $west
 * @property string $capital
 * @property string $continentName
 * @property string $continent
 * @property string $areaInSqKm
 * @property string $languages
 * @property string $isoAlpha3
 * @property integer $geonameId
 */
class Countries extends CActiveRecord {

    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return Countries the static model class
     */
    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return '{{countries}}';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('geonameId', 'numerical', 'integerOnly' => true),
            array('countryCode, fipsCode, continent', 'length', 'max' => 2),
            array('countryName', 'length', 'max' => 45),
            array('currencyCode, isoAlpha3', 'length', 'max' => 3),
            array('population, areaInSqKm', 'length', 'max' => 20),
            array('isoNumeric', 'length', 'max' => 4),
            array('north, south, east, west, capital, languages', 'length', 'max' => 30),
            array('continentName', 'length', 'max' => 15),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('idCountry, countryCode, countryName, currencyCode, population, fipsCode, isoNumeric, north, south, east, west, capital, continentName, continent, areaInSqKm, languages, isoAlpha3, geonameId', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            'idCountry' => 'Id Country',
            'countryCode' => 'Country Code',
            'countryName' => 'Country Name',
            'currencyCode' => 'Currency Code',
            'population' => 'Population',
            'fipsCode' => 'Fips Code',
            'isoNumeric' => 'Iso Numeric',
            'north' => 'North',
            'south' => 'South',
            'east' => 'East',
            'west' => 'West',
            'capital' => 'Capital',
            'continentName' => 'Continent Name',
            'continent' => 'Continent',
            'areaInSqKm' => 'Area In Sq Km',
            'languages' => 'Languages',
            'isoAlpha3' => 'Iso Alpha3',
            'geonameId' => 'Geoname',
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search() {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;
        $criteria->compare('idCountry', $this->idCountry);
        $criteria->compare('countryCode', $this->countryCode, true);
        $criteria->compare('countryName', $this->countryName, true);
        $criteria->compare('currencyCode', $this->currencyCode, true);
        $criteria->compare('population', $this->population, true);
        $criteria->compare('fipsCode', $this->fipsCode, true);
        $criteria->compare('isoNumeric', $this->isoNumeric, true);
        $criteria->compare('north', $this->north, true);
        $criteria->compare('south', $this->south, true);
        $criteria->compare('east', $this->east, true);
        $criteria->compare('west', $this->west, true);
        $criteria->compare('capital', $this->capital, true);
        $criteria->compare('continentName', $this->continentName, true);
        $criteria->compare('continent', $this->continent, true);
        $criteria->compare('areaInSqKm', $this->areaInSqKm, true);
        $criteria->compare('languages', $this->languages, true);
        $criteria->compare('isoAlpha3', $this->isoAlpha3, true);
        $criteria->compare('geonameId', $this->geonameId);

        return new CActiveDataProvider($this, array(
            'criteria' => $criteria,
        ));
    }

    public static function GetCurrency() {
        $countryCode = '';
        if (Yii::app()->user->isGuest) {
            //get country code of user Guest
            $ipAdd = Yii::app()->ipAdd->getInfo();
            $countryCode = $ipAdd->countryCode;
        } else {
            //get country of user
            $uid = Yii::app()->user->id;

            $userAdd = UserAddress::model()->findByAttributes(array('uid' => $uid, 'status' => 1));
            /*
              echo '<PRE>';
              print_r($userAdd);
              echo '</PRE>';
              die(''); */
            $countryCode = $userAdd->countryCode;
        }
        //echo $countryCode;
        //die('vinh bug');
        if ($countryCode !== '') {
            $currency = Countries::model()->findByAttributes(array('countryCode' => $countryCode));
            $currencyCode = $currency->currencyCode;
        } else {
            $currencyCode = 'USD';
        }
        //kiem tra currency cho những truong hop khong nam trong khai bao
        //vi chi chap nhan nhung currency trong table gxc_currency
        return Currency::checkCurrency($currencyCode);
    }

    public static function GetCurrencyByCountryCode($iso2) {
        $country = Countries::model()->findByAttributes(array('countryCode' => $iso2));
        $currency = $country->currencyCode;

        if (strtoupper($currency) === "VND") {
            return "VND";
        } else if (strtoupper($currency) === "AUD") {
            return "AUD";
        } else {
            return "USD";
        }
    }

    public static function GetCountryName($iso2) {
        $country = Countries::model()->findByAttributes(array('countryCode' => $iso2));
        return $country->countryName;
    }

}
